<template>
	<view class="">
		<view class="d-flex flex-wrap a-center j-sb pl15 pr15">
			<view class="mb15" v-for="(item, index) in cardDataStatus2" :key="index" @click="popupOpen(item, index, '0')">
				<view class="content_item p15 boxSizing" :class="classroomDataBgColor[item.socketColor]">
					<view class="d-flex">
						<view class="d-flex-center">
							<u--image :showLoading="true" src="http://36.138.43.18:8081/upload/486720241109141148.png" width="36px"
								height="36px"></u--image>
						</view>
					</view>
					<view class="mt10">
						<view class="rowName">{{index==0 ? '1号楼太阳能总控': '3号楼太阳能总控'}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="d-flex flex-wrap a-center j-sb pl15 pr15">
			<view class="mb15" v-for="(item, index) in cardDataStatus3" :key="index" @click="popupOpen(item, index, '1')">
				<view class="content_item p15 boxSizing" :class="classroomDataBgColor[item.socketColor]">
					<view class="d-flex">
						<view class="d-flex-center">
							<u--image :showLoading="true" :src="carDataBgImg[index]" width="36px" height="36px"></u--image>
						</view>
					</view>
					<view class="mt10">
						<view class="rowName">{{cardTitle[index]}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="d-flex flex-wrap a-center j-sb pl15 pr15">
			<view class="mb15" v-for="(item, index) in cardDataStatus5" :key="index" @click="popupOpen(item, index, '2')">
				<view class="content_item p15 boxSizing" :class="classroomDataBgColor[item.socketColor]">
					<view class="d-flex">
						<view class="d-flex-center">
							<u--image :showLoading="true" :src="carDataBgImg2[index]" width="36px" height="36px"></u--image>
						</view>
					</view>
					<view class="mt10">
						<view class="rowName">{{cardTitle2[index]}}</view>
					</view>
				</view>
			</view>
		</view>

		<u-modal :show="modalShow1" :title="modalTitle1" :content='modalContent1' :showConfirmButton="true"
			:showCancelButton="true" @confirm="modalConfirm" @cancel="modalShow1 = false"></u-modal>
	</view>
</template>

<script>
	import {
		queryTwoListSunControlByStatus,
		queryThreeListSunControlByStatus,
		queryFiveListSunControlByStatus,
	} from "@/api/dormBuilding/solarEnergy.js"

	import {
		queryControlListByApp,
		makeDeviceControlByApp,
		queryControlListV3ByApp,
		batchControlByApp
	} from "@/api/index.js"

	export default {
		data() {
			return {
				userInfo: {},
				timeout: [],

				cardDataStatus2: {},
				cardDataStatus3: [],
				cardDataStatus5: [],

				cardTitle: ['东1空气能开关', '东2空气能开关', '东3空气能开关', '东4空气能开关', '西1空气能开关', '西2空气能开关',
					'西3空气能开关', '南北循环泵开关 ', '南北水箱+继电器开关', '继电器开关', '北水箱开关', '南水箱开关'
				],
				carDataBgImg: [
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/252220241609141629.png',
					'http://36.138.43.18:8081/upload/208920241709141705.png',
					'http://36.138.43.18:8081/upload/106920241709141747.png',
					'http://36.138.43.18:8081/upload/865820241809141817.png',
					'http://36.138.43.18:8081/upload/865820241809141817.png',
				],


				cardTitle2: ['北空气能开关', '南空气能开关', '水箱+循环泵开关', '水箱控制器开关'],
				carDataBgImg2: [
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/574720241409141453.png',
					'http://36.138.43.18:8081/upload/208920241709141705.png',
					'http://36.138.43.18:8081/upload/865820241809141817.png',
				],
				classroomDataBgColor: ['bgColor_info', 'bgColor_success', 'bgColor_danger', 'bgColor_blue',
					'bgColor_warning', 'bgColor_red', 'bgColor_orange'
				],

				modalShow1: false,
				modalTitle1: "",
				modalContent1: "",
				apiObj: {},

				type: ""
			}
		},
		created() {
			this.userInfo = uni.getStorageSync('userInfo')
		},
		mounted() {
			this.queryTwoListSunControlByStatus()
			this.queryThreeListSunControlByStatus()
			this.queryFiveListSunControlByStatus()

			this.setupTimer()
		},
		methods: {
			// 设置定时器
			setupTimer() {
				let _this = this;
				_this.timeout[0] = setInterval(() => {
					_this.queryTwoListSunControlByStatus()
					_this.queryThreeListSunControlByStatus()
					_this.queryFiveListSunControlByStatus()
				}, 15000);
			},
			// 清除定时器
			clearTimer() {
				let _this = this;
				if (_this.timeout.length > 0) {
					_this.timeout.forEach(item => {
						clearInterval(item);
					})
				}
			},

			queryTwoListSunControlByStatus() {
				queryTwoListSunControlByStatus().then(res => {
					this.cardDataStatus2 = res.object
				})
			},
			queryThreeListSunControlByStatus() {
				queryThreeListSunControlByStatus().then(res => {
					this.cardDataStatus3 = res.object
				})
			},
			queryFiveListSunControlByStatus() {
				queryFiveListSunControlByStatus().then(res => {
					this.cardDataStatus5 = res.object
				})
			},


			// 开关和分闸
			popupOpen(data, roomName, type) {
				this.type = type

				this.dataItem = data

				let tempTitle = ""
				switch (type) {
					case '0':
						tempTitle = roomName == 0 ? '1号楼太阳能总控' : '3号楼太阳能总控'
						break;
					case "1":
						tempTitle = this.cardTitle[roomName]
						break;
					case '2':
						tempTitle = this.cardTitle2[roomName]
						break;
					default:
						tempTitle = '设备'
						break;
				}
				this.modalTitle1 = tempTitle

				let obj = {}
				if (data.deviceList[data.deviceList.length - 2].deviceValue == '0') {
					obj = {
						command: '65365',
						commandName: '合闸',
						parameterCode: 'CTRL',
					}
					this.modalContent1 = "确定执行合闸操作吗, 是否继续?"
				} else {
					obj = {
						command: '65280',
						commandName: '分闸',
						parameterCode: 'CTRL',
					}
					this.modalContent1 = "确定执行分闸操作吗, 是否继续?"
				}
				this.apiObj = obj

				this.modalShow1 = true
			},

			modalConfirm(e) {
				let that = this

				let obj = {
					userCode: this.userInfo.userCode,
					userName: this.userInfo.userName,
					deviceCode: this.dataItem.deviceCode,
					parameterCode: this.apiObj.parameterCode,
					command: this.apiObj.command,
					commandName: this.apiObj.commandName,
				}

				that.modalShow1 = false
				
				makeDeviceControlByApp(obj).then(res => {
					if (res.status == 200 && (res.message == '指令发送成功' || res.message == '指令发送成功等待核验')) {

						that.$refs.uToast.show({
							type: 'loading',
							title: '正在加载',
							message: "正在加载",
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/loading.png',
							duration: 2000,
							complete() {
								if (that.type == 1) {
									that.queryThreeListSunControlByStatus()
								} else if (that.type == 2) {
									that.queryFiveListSunControlByStatus()
								} else if (that.type == 3) {
									that.queryFiveListControlByStatus()
								} else if (that.type == 5) {
									that.querySixListControlByStatusV2()
								} else if (that.type == 6) {
									that.querySevenListControlByStauts()
								} else {
									that.queryEightListControlByStauts()
								}

								uni.showToast({
									duration: 1000,
									icon: "success",
									title: "指令发送成功"
								})
							}
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bgColor_info {
		background: #787878;
	}

	.bgColor_success {
		background: #1BBC7C;
	}

	.bgColor_danger {
		background: #AD5679;
	}

	.bgColor_blue {
		background: #5589F2;
	}

	.bgColor_warning {
		background: #E8CB58;
	}

	.bgColor_red {
		background: #E14B4B;
	}

	.bgColor_orange {
		background: #E46F1C;
	}

	.content_item {
		width: 334rpx;
		border-radius: 16rpx;
		box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.10);

		.icon-kaiguan-kai {
			color: #5589F2;
		}

		.rowName {
			font-size: 28upx;
			color: #ffffff;
			font-family: Source Han Sans CN, Source Han Sans CN-400;
		}

		.rowValue {
			font-size: 28upx;
			color: #ffffff;
		}
	}
</style>